import time
import uuid
import re
import hashlib
import logging
logging.basicConfig(level=logging.INFO)


def next_id():
    return '%015d%s000' % (int(time.time() * 1000), uuid.uuid4().hex)


def object2dict(obj):
    pr = {}
    for name in dir(obj):
        value = getattr(obj, name)
        if not name.startswith('__') and not name.startswith(
                "_") and not callable(value):
            pr[name] = value
    return pr


def checkEmail(email):
    _RE_EMAIL = re.compile(
        r'^[a-z0-9\.\-\_]+\@[a-z0-9\-\_]+(\.[a-z0-9\-\_]+){1,4}$')
    return _RE_EMAIL.match(email)


def checkPwd(pwd):
    _RE_SHA1 = re.compile(r'^[0-9a-f]{40}$')
    return _RE_SHA1.match(pwd)


def genaratePwd(uid, password):
    sha1_passwd = '%s:%s' % (uid, password)
    return hashlib.sha1(sha1_passwd.encode('utf-8')).hexdigest()


def avataHeadImg(email):
    return 'http://www.gravatar.com/avatar/%s?d=mm&s=120' % hashlib.md5(
        email.encode('utf-8')).hexdigest()
